
import webSocket from '@ohos.net.webSocket';
import router from '@system.router';
let ws = webSocket.createWebSocket();
let url;

export default {
    data: {
        userId: '1482650678974943233',
        userId1:'1470585127007473665',
        token:'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiMCIsImV4cCI6MTY0NTI0OTQ3MywidXNlcklkIjoiMTQ4MjY1MDY3ODk3NDk0MzIzMyIsInVzZXJuYW1lIjoiUVHlhYnmo43oioLlv6vkuZAifQ.j-kqvMGa2CuRBVjE72BYp81Pxf_--JUqrgFjbsZMWiM',
        message: '',
        name:'1',
        array: [],
        replyMessage: [],
        allMessage: [],
        bothMessCss: {messChange:'', cssChange: ''},
        messChange: [],
        cssChange: 'listItemRight'
    },
    getMessage(e){
        this.message=e.value;
    },
    onInit(){
        console.log("-----=="+this.userId);
//        this.replyMessage.push("df");
        console.log("-----=="+this.replyMessage[0]);

        url = "ws://dreamwake.top:8888/chat/" + this.userId+'?token='+this.token;
        ws.connect(url, (err, value) => {

            if (!err) {
                console.log("connect success pig------"+value);
            } else {
                console.log("connect fail, err:" + JSON.stringify(err))
            }
        });
        ws.on('open', (err, value) => {
            console.log("on open, status-----:" + value.status + ", message:" + JSON.stringify(value));
        });

        ws.on('message', (err, value) => {
            let result = JSON.parse(value);
            if(result.type == 0 && result.messageType == 4) {
                this.bothMessCss = {messChange:'', cssChange: ''};
                this.bothMessCss.cssChange = 'listItem';
                this.bothMessCss.messChange = result.textMessage;
                this.allMessage.push(this.bothMessCss);
            }
            console.log("on message, message----:" + value);
            console.log("on  -----r" + JSON.stringify(this.allMessage));

            if(err) {
                console.log("message fail, err:" + JSON.stringify(err))
            }
        });


    },

    backClick(){
        console.log("name------:"+this.name);
      router.back();
    },

    send(){

        var mesData ={
            "message":this.message,//消息内容
            "type":0,//消息类型 0-文字 1-图片 2-语音
            "userId":this.userId,//发送者id
            "toUserId":this.userId1,//接受者id
        }
//        console.log("sendllllll---");
//        if(result.messageType == 3 && result.onlineFriendNumber > 0){

        ws.send(JSON.stringify(mesData), (err, value) => {
            if (!err) {
                this.bothMessCss = {messChange:'', cssChange: ''};
                this.bothMessCss.cssChange = 'listItemRight';
                this.array.push(this.message);
                this.bothMessCss.messChange = this.message;
                this.allMessage.push(this.bothMessCss);
                console.log("on  -----send" + JSON.stringify(this.allMessage));
                console.log("send success----"+value+",message:---"+this.message);
                console.log("send fail, err-----:" + JSON.stringify(err));
                this.message='';
            } else {
                console.log("send fail, err:" + JSON.stringify(err));
            }
        });

        ws.on('error', (err) => {

            console.info("on error, error:" + JSON.stringify(err));

        });

    },
}







